package com.feiyu.stackqueue;

import java.util.Objects;

/**
 * @Author nanshouxiao@teacher.com.cn
 * @Date 2017-10-27 17:09
 */
public class PriorityQueue<T extends Comparable> extends BaseQueue<T> {
    public PriorityQueue() {
        super();
    }

    public PriorityQueue(int capacity) {
        super(capacity);
    }

    @Override
    public T push(T t) {
        if (isFull()) {
            System.out.println("队列已满，插入失败");
            return null;
        }
        start--;
        if (start < 0) {
            start = capacity + start;
        }
        if (start >= capacity || start < 0) {
            start = start%capacity;
        }
        for(int i = 1; i <= size; i++) {
            T temp = (T) datas[(start + i) % capacity];
            if (temp != null && t.compareTo(temp) < 0) {
                datas[(start + i - 1) % capacity] = temp;
                continue;
            }
            datas[(start + i - 1) % capacity] = t;
            size++;
            return t;
        }
        datas[(start + size) % capacity] = t;
        size++;
        return t;
    }
}
